﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace TrackingAssist.WebTrends
{
    public class WebTrendsSpecUtility
    {
        public static void SetSpecAtlasItem(WebTrendsItem item, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                item.Value = string.Empty;
                item.IsDynamic = false;
            }
            else
            {
                value = value.Trim();
                item.Value = value;
                if (value.StartsWith(WebTrendsConsts.SPEC_FLAG_DYMATIC))
                    item.IsDynamic = true;
            }
        }

        public static void CompareActualWithSpec(WebTrends actualAtlas)
        {

            if (WebTrendsView.WebTrendsSpecList != null && WebTrendsView.WebTrendsSpecList.Count > 0)
            {
                string actualId = null;
                string specId=null;
                if (actualAtlas.Id !=null && Convert.ToInt16(actualAtlas.Id.LastIndexOf("_")) > 0)
                    actualId = actualAtlas.Id.Substring(Convert.ToInt16(actualAtlas.Id.LastIndexOf("_")) + 1);

                foreach (WebTrends specWebTrends in WebTrendsView.WebTrendsSpecList)
                {

                    if (specWebTrends.Id != null && Convert.ToInt16(specWebTrends.Id.LastIndexOf("_")) > 0)
                        specId = specWebTrends.Id.Substring(Convert.ToInt16(specWebTrends.Id.LastIndexOf("_")) + 1);

                    if (!string.Equals(actualId, specId))
                            continue;
                    if (IsAtlasMatch(actualAtlas, specWebTrends))
                    {
                        actualAtlas.Match = MatchResult.Match;
                        return;
                    }
                    else
                    {
                        actualAtlas.Match = MatchResult.NotMatch;
                        return;
                    }
                }
                actualAtlas.Match = MatchResult.UnKnown;
                
            }
            else
            {
                actualAtlas.Match = MatchResult.UnKnown;
            }
        }

        private static bool IsAtlasMatch(WebTrends actualWebTrends, WebTrends specWebTrends)
        {
            //compare every item, and set the value to item.Ismatch 
            if (IsAtlasItemMatch(actualWebTrends.Section.Value, specWebTrends.Section.Value))
                actualWebTrends.Section.IsMatch = true;

            if (IsAtlasItemMatch(actualWebTrends.Location.Value, specWebTrends.Location.Value))
                actualWebTrends.Location.IsMatch = true;

            if (IsAtlasItemMatch(actualWebTrends.Action.Value, specWebTrends.Action.Value))
                actualWebTrends.Action.IsMatch = true;

            if (IsAtlasItemMatch(actualWebTrends.ActionName.Value, specWebTrends.ActionName.Value))
                actualWebTrends.ActionName.IsMatch = true;

            if (IsAtlasItemMatch(actualWebTrends.Var1.Value, specWebTrends.Var1.Value))
                actualWebTrends.Var1.IsMatch = true;

            if (IsAtlasItemMatch(actualWebTrends.Var2.Value, specWebTrends.Var2.Value))
                actualWebTrends.Var2.IsMatch = true;

            if (IsAtlasItemMatch(actualWebTrends.Var3.Value, specWebTrends.Var3.Value))
                actualWebTrends.Var3.IsMatch = true;


            actualWebTrends.Section.IsDynamic = specWebTrends.Section.IsDynamic;
            actualWebTrends.Location.IsDynamic = specWebTrends.Location.IsDynamic;
            actualWebTrends.Action.IsDynamic = specWebTrends.Action.IsDynamic;
            actualWebTrends.ActionName.IsDynamic = specWebTrends.ActionName.IsDynamic;
            actualWebTrends.Var1.IsDynamic = specWebTrends.Var1.IsDynamic;
            actualWebTrends.Var2.IsDynamic = specWebTrends.Var2.IsDynamic;
            actualWebTrends.Var3.IsDynamic = specWebTrends.Var3.IsDynamic;

            if (actualWebTrends.Section.IsMatch && actualWebTrends.Location.IsMatch &&
                actualWebTrends.Action.IsMatch && actualWebTrends.ActionName.IsMatch &&
                actualWebTrends.Var1.IsMatch && actualWebTrends.Var2.IsMatch &&
                actualWebTrends.Var3.IsMatch
                )
            {
                actualWebTrends.Id = specWebTrends.Id;
                actualWebTrends.Description = specWebTrends.Description;
                return true;
            }

            return false;
        }

        private static bool IsAtlasItemMatch(string actualItem, string specItem)
        {
            if (string.IsNullOrEmpty(actualItem) && string.IsNullOrEmpty(specItem))
                return true;

            //if(Regex.IsMatch(actualItem,AtlasConsts.Reg_encode))
            //    return false;

            if (specItem.StartsWith(WebTrendsConsts.SPEC_FLAG_DYMATIC))
            {
                string type = specItem.Substring(3);
                string strRegex = string.Empty;
                switch(type)
                {
                    case "[s]":
                        //only char
                        strRegex = WebTrendsConsts.Reg_Str;
                        break;
                    case "[n]":
                         //only number
                        strRegex = WebTrendsConsts.Reg_Num;     
                         break;
                    case "[t]":
                         //number,char and "_"
                         strRegex = WebTrendsConsts.Reg_Str_Num;           
                         break;
                    case "[u]":
                         //internet url
                         strRegex = WebTrendsConsts.Reg_Url;
                         break;
                    default:
                         break;
                }
                return Regex.IsMatch(actualItem, strRegex);
            }
            else
            {
                return (string.Compare(actualItem, specItem, true) == 0);
            }
        }
    }
}
